package controller

import (
	"encoding/json"
	"gugojudger/utils/ojchannel"
	"gugojudger/utils/ojresult"
	"net/http"
	"time"
)

var superChan *ojchannel.OJChannel

// StartHttpControl 启动http服务
func StartHttpControl(C *ojchannel.OJChannel) {
	superChan = C
	http.HandleFunc("/info", handleInfo)
	http.HandleFunc("/vjudger", handleJudge)
	http.HandleFunc("/forcestop", handleFatalStop)
	http.ListenAndServe(":8880", nil)
}

//force stop the GGO
func handleFatalStop(resp http.ResponseWriter, req *http.Request) {
	var res = ojresult.NewResult()
	res.SetMessage(200, true)
	res.AddData("msg", "successful force stop the GGO service")

	superChan.C <- ojchannel.FastLog(5, "Force Stop Service at "+time.Now().Format("2006-01-02 15:04:05 MST"))

	resJson, _ := json.Marshal(res)
	resp.Write(resJson)
}
